在前四篇,我們建構了 LangChain Agent 的完整架構,今天我們要深入探討智能助手的「創意核心」:內容生成引擎。
這個引擎能夠根據用戶的需求,智能生成各種類型的內容,從技術文章到創意小說,從學習心得到商業報告。
內容生成引擎提供:
def generate_content_tool(self, topic: str) -> str:
"""生成指定主題的創意內容"""
print(f"[DEBUG] ===== generate_content_tool 被調用了! =====")
print(f"[DEBUG] 收到的主題: '{topic}'")
try:
# 解析用戶需求
if not topic.strip():
return "請提供要生成內容的主題。"
# 根據關鍵詞判斷內容類型
content_type = "文章"
if "心得" in topic or "感想" in topic or "評價" in topic:
content_type = "心得感想"
elif "故事" in topic or "小說" in topic:
content_type = "故事"
elif "報告" in topic or "分析" in topic:
content_type = "分析報告"
elif "介紹" in topic:
content_type = "介紹文章"
elif "信" in topic or "信件" in topic:
content_type = "信件"
# 生成詳細提示
if content_type == "心得感想":
prompt = f"""請為以下主題寫一篇詳細的心得感想(約300-500字):
主題:{topic}
請包含以下要素:
1. 個人的感受和印象
2. 具體的例子或情節
3. 深層的思考和啟發
4. 結論和總結
請用繁體中文書寫,語調要自然親切。"""
else:
prompt = f"""請為以下主題生成一篇詳細的{content_type}(約300-500字):
主題:{topic}
請確保內容:
1. 結構完整清晰
2. 內容豐富有趣
3. 語言流暢自然
4. 符合主題要求
請用繁體中文書寫。"""
print(f"[DEBUG] 準備使用Gemini生成內容,content_type: {content_type}")
print(f"[DEBUG] 生成的prompt長度: {len(prompt)}")
# 使用Gemini生成內容
response = self.content_model.generate_content(prompt)
generated_content = response.text
print(f"[DEBUG] Gemini回傳內容長度: {len(generated_content)}")
print(f"[DEBUG] Gemini回傳內容前100字: {generated_content[:100]}...")
# 轉換為繁體中文
generated_content = self.opencc_converter.convert(generated_content)
print(f"[DEBUG] 轉換繁體中文後長度: {len(generated_content)}")
# 保存生成的內容供後續使用
if self.app_instance:
import time
self.app_instance.last_generated_content = generated_content
self.app_instance.last_generated_time = time.time()
print(f"[DEBUG] 已保存生成的內容,長度: {len(generated_content)},時間戳: {self.app_instance.last_generated_time}")
print(f"[DEBUG] 準備回傳完整內容,長度: {len(generated_content)}")
return generated_content
except Exception as e:
print(f"[DEBUG] generate_content_tool發生錯誤: {type(e).__name__}: {e}")
# 當API配額用完時,提示用戶
if "quota" in str(e).lower() or "429" in str(e):
return f"內容生成功能暫時不可用(API配額已用完)。請稍後重試或考慮升級API方案。原始錯誤:{str(e)}"
else:
return f"生成內容時發生錯誤: {e}"
# 根據關鍵詞判斷內容類型
content_type = "文章"
if "心得" in topic or "感想" in topic or "評價" in topic:
content_type = "心得感想"
elif "故事" in topic or "小說" in topic:
content_type = "故事"
elif "報告" in topic or "分析" in topic:
content_type = "分析報告"
elif "介紹" in topic:
content_type = "介紹文章"
elif "信" in topic or "信件" in topic:
content_type = "信件"
# 生成詳細提示
if content_type == "心得感想":
prompt = f"""請為以下主題寫一篇詳細的心得感想(約300-500字):
主題:{topic}
請包含以下要素:
1. 個人的感受和印象
2. 具體的例子或情節
3. 深層的思考和啟發
4. 結論和總結
請用繁體中文書寫,語調要自然親切。"""
else:
prompt = f"""請為以下主題生成一篇詳細的{content_type}(約300-500字):
主題:{topic}
請確保內容:
1. 結構完整清晰
2. 內容豐富有趣
3. 語言流暢自然
4. 符合主題要求
請用繁體中文書寫。"""
# 保存生成的內容到應用程式實例
if self.app_instance:
import time
self.app_instance.last_generated_content = content
self.app_instance.last_generated_time = time.time()
print(f"[DEBUG] 生成內容已保存,字數: {len(content)}")
保存特色:
# 生成工作流程
內容生成工具 → 檢查內容狀態 → 將已生成內容寫入Word → 修改Word內容 → 儲存Word文件
↓ ↓ ↓ ↓ ↓
創建內容 狀態確認 寫入文件 編輯修改 完成保存
self.opencc_converter = OpenCC('s2t') # Simplified to Traditional
# 在每次內容生成後自動轉換
analysis_text = self.opencc_converter.convert(analysis.text)
content = self.opencc_converter.convert(response.text)
# 根據關鍵詞自動判斷內容類型
content_type_mapping = {
"心得感想": ["心得", "感想", "評價"],
"故事": ["故事", "小說"],
"分析報告": ["報告", "分析"],
"介紹文章": ["介紹"],
"信件": ["信", "信件"]
}
# 針對心得感想類型的特殊處理
if content_type == "心得感想":
prompt = f"""請為以下主題寫一篇詳細的心得感想(約300-500字):
主題:{topic}
請包含以下要素:
1. 個人的感受和印象
2. 具體的例子或情節
3. 深層的思考和啟發
4. 結論和總結
請用繁體中文書寫,語調要自然親切。"""
# 智能錯誤處理
if "quota" in str(e).lower() or "429" in str(e):
return f"內容生成功能暫時不可用(API配額已用完)。請稍後重試或考慮升級API方案。原始錯誤:{str(e)}"
print(f"[DEBUG] ===== generate_content_tool 被調用了! =====")
print(f"[DEBUG] 收到的主題: '{topic}'")
print(f"[DEBUG] 準備使用Gemini生成內容,content_type: {content_type}")
print(f"[DEBUG] Gemini回傳內容長度: {len(generated_content)}")
print(f"[DEBUG] 已保存生成的內容,長度: {len(generated_content)},時間戳: {self.app_instance.last_generated_time}")
# 長度檢驗
if len(content) < 200:
return "生成的內容過短,請提供更詳細的主題描述。"
# 結構檢驗
if not ("。" in content or "!" in content or "?" in content):
return "生成的內容結構異常,請重新嘗試。"
# 自動評估內容豐富度
richness_indicators = [
"段落數量 >= 3",
"包含具體例子",
"有邏輯結構",
"語言表達豐富"
]
# 在生成結果中提供使用建議
result = f"""
✅ 內容生成完成!
{content}
💡 提示:內容已生成完成,您可以說「寫入Word」將內容寫入文件。
"""
內容生成引擎是智能助手的「創意大腦」,它:
這個引擎讓 AI 助手不僅能夠理解和執行指令,更能夠進行創意思考和內容創作,真正成為用戶的智能創作夥伴。
下一篇,我們將探討檔案上傳與智能分析系統,看看 AI 如何處理和分析各種格式的檔案。